package com.lgl.maker.generator;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import com.lgl.maker.generator.file.FileGenerator;
import com.lgl.maker.generator.file.StaticGenerator;
import com.lgl.maker.meta.Meta;
import com.lgl.maker.meta.MetaManager;
import freemarker.template.TemplateException;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * @author 良哥
 * @verison FileGenerator.java.ftl.0
 */
public class MainGenerator {
    public static void main(String[] args) throws TemplateException, IOException, InterruptedException {
        Meta meta = MetaManager.getMetaManager();

        // 项目根目录
        String property = System.getProperty("user.dir").replace("\\", "/");
        // 输出路径
        String outputPath = property + "/generator/" + meta.getName();
        // 没有目录则创建目录
        if (!FileUtil.exist(outputPath)) {
            FileUtil.mkdir(outputPath);
        }

        // 资源根目录
        ClassPathResource classPathResource = new ClassPathResource("");
        String resourcePath = classPathResource.getAbsolutePath();


        String inputFile;
        String outputFilePath;


        // 生成资源目录
        String sourceRootPath = meta.getFileConfig().getInputRootPath();
        String sourcePath = outputPath + "/.source";
        FileUtil.copy(sourceRootPath, sourcePath, true);


        inputFile = resourcePath + "templates/java/model/MainTemplateModel.java.ftl";

        // 配置中的包名
        String basePath = meta.getBasePackage().replace(".", "/");

        // 输出(写入)文件
        outputFilePath = outputPath + "/src/main/java/" + basePath + "/model/MainTemplateModel.java";

        System.out.println(inputFile);
        System.out.println(outputFilePath);
        FileGenerator.doGen(inputFile, outputFilePath, meta);

        // 输入(读取)目录
//        inputFile = resourcePath + "templates/java/MainTemplate.java.ftl";
//
//        // 输出(写入)文件
//        outputFilePath = outputPath + "/src/main/java/" + basePath + "/MainTemplate.java.ftl";
//
//        System.out.println(inputFile);
//        System.out.println(outputFilePath);
//        StaticGenerator.copyFilesByHutool(inputFile, outputFilePath, true);


        // ConfigCommand.java
        inputFile = resourcePath + "templates/java/cli/command/ConfigCommand.java.ftl";
        outputFilePath = outputPath + "/src/main/java/" + basePath + "/cli/command/ConfigCommand.java";
        FileGenerator.doGen(inputFile, outputFilePath, meta);

        // GenerateCommand.java
        inputFile = resourcePath + "templates/java/cli/command/GenerateCommand.java.ftl";
        outputFilePath = outputPath + "/src/main/java/" + basePath + "/cli/command/GenerateCommand.java";
        FileGenerator.doGen(inputFile, outputFilePath, meta);

        // ListCommand.java
        inputFile = resourcePath + "templates/java/cli/command/ListCommand.java.ftl";
        outputFilePath = outputPath + "/src/main/java/" + basePath + "/cli/command/ListCommand.java";
        FileGenerator.doGen(inputFile, outputFilePath, meta);

        // CommandExecutor.java
        inputFile = resourcePath + "templates/java/cli/CommandExecutor.java.ftl";
        outputFilePath = outputPath + "/src/main/java/" + basePath + "/cli/CommandExecutor.java";
        FileGenerator.doGen(inputFile, outputFilePath, meta);

        // Main.java
        inputFile = resourcePath + "templates/java/Main.java.ftl";
        outputFilePath = outputPath + "/src/main/java/" + basePath + "/Main.java";
        FileGenerator.doGen(inputFile, outputFilePath, meta);

        // FileGenerator.java
        inputFile = resourcePath + "templates/java/generator/FileGenerator.java.ftl";
        outputFilePath = outputPath + "/src/main/java/" + basePath + "/generator/FileGenerator.java";
        FileGenerator.doGen(inputFile, outputFilePath, meta);

        // StaticGenerator.java
        inputFile = resourcePath + "templates/java/generator/StaticGenerator.java.ftl";
        outputFilePath = outputPath + "/src/main/java/" + basePath + "/generator/StaticGenerator.java";
        FileGenerator.doGen(inputFile, outputFilePath, meta);

        // StaticGenerator.java
        inputFile = resourcePath + "templates/java/generator/StaticGenerator.java.ftl";
        outputFilePath = outputPath + "/src/main/java/" + basePath + "/generator/StaticGenerator.java";
        FileGenerator.doGen(inputFile, outputFilePath, meta);

        // DynamicGenerator.java
        inputFile = resourcePath + "templates/java/generator/DynamicGenerator.java.ftl";
        outputFilePath = outputPath + "/src/main/java/" + basePath + "/generator/DynamicGenerator.java";
        FileGenerator.doGen(inputFile, outputFilePath, meta);

        // pom.xml --> for build
        inputFile = resourcePath + "templates/pom.xml.ftl";
        outputFilePath = outputPath + "/pom.xml";
        FileGenerator.doGen(inputFile, outputFilePath, meta);

        //
        JarGenerator.doGen(outputPath);
        // 脚本位置
        String ScriptShellPath = outputPath + "/generator";
        // jar包位置
        String JarPath = "target/" + meta.getName() + "-" + meta.getVersion() + "-jar-with-dependencies.jar";
        ScriptGenerator.doGen(ScriptShellPath, JarPath);


        // 生成精简版的程序（产物包）
        String distOutputPath = outputPath + "-dist";
        // - 拷贝 jar 包
        String targetAbsolutePath = distOutputPath + File.separator + "target";
        FileUtil.mkdir(targetAbsolutePath);
        String jarAbsolutePath = outputPath + File.separator + JarPath;
        FileUtil.copy(jarAbsolutePath, targetAbsolutePath, true);

        // - 拷贝脚本文件
        FileUtil.copy(ScriptShellPath + ".bat", distOutputPath, true);
        // - 拷贝源模板文件
        FileUtil.copy(sourcePath, distOutputPath, true);
    }


}
